﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Collections.ObjectModel;
using Medecine.DataAccess.Observations;
using Medecine.Interface.Main;
using GalaSoft.MvvmLight.Messaging;
using Medecine.Dbo;
using Medecine.Interface.Utils;

namespace Medecine.Interface.Observations
{
	public class ObservationListViewModel : ViewModelBase
	{
		public ObservationListViewModel(int patientId, Observation[] observationList)
		{
			_patientId = patientId;

			foreach (Observation observation in observationList)
			{
				ObservationList.Add(new ObservationViewModel(observation));
			}

			_addObservationCommand = new RightCommand(AddObservation, ViewModelLocator.MainStatic.CanInvokeCommand);
		}

		private readonly int _patientId;

		private readonly ObservableCollection<ObservationViewModel> _observationList = new ObservableCollection<ObservationViewModel>();
		public ObservableCollection<ObservationViewModel> ObservationList { get { return _observationList; } }

		#region Add observation

		private AddObservationViewModel _addObservationViewModel = null;

		private readonly RightCommand _addObservationCommand = null;
		public RightCommand AddObservationCommand { get { return _addObservationCommand; } }

		private void AddObservation()
		{
			_addObservationViewModel = new AddObservationViewModel(_patientId);
			Messenger.Default.Send(new GoToViewMessage(ViewName.AddObservation, _addObservationViewModel, AddObservationCompleted));
		}

		private void AddObservationCompleted()
		{
			if (_addObservationViewModel.Cancelled == false)
			{
				_observationList.Add(new ObservationViewModel(_addObservationViewModel.Observation));
			}

			_addObservationViewModel.Cleanup();
			_addObservationViewModel = null;
		}

		#endregion // Add observation
	}
}
